package com.xxl.job.admin.controller;

import com.xxl.job.kettle.core.dto.EnumInfoDTO;
import com.xxl.job.kettle.core.entity.KDatabaseType;
import com.xxl.job.kettle.core.service.KDatabaseTypeService;
import com.xxl.job.kettle.enums.RepTypeEnum;
import com.xxl.job.kettle.util.EnumUtil;
import lombok.RequiredArgsConstructor;
import org.pentaho.di.core.database.DatabaseMeta;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 实现功能：
 *
 * @author 作者[zk]
 * @version 1.0.00  2022-12-01 11:16
 */
@Controller
@RequestMapping("/jobRepository")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class jobRepositoryController {

    private final KDatabaseTypeService databaseTypeService;

    @RequestMapping
    public String index(Model model) {
        List<EnumInfoDTO> repTypeList = EnumUtil.getEnumInfo(RepTypeEnum.values());
        model.addAttribute("repTypeList", repTypeList);

        List<KDatabaseType> dbTypeList = databaseTypeService.list();
        List<EnumInfoDTO> dbDtoList = dbTypeList.stream().map(databaseTypeRes -> {
            EnumInfoDTO dto = new EnumInfoDTO();
            dto.setCode(databaseTypeRes.getCode());
            dto.setValue(databaseTypeRes.getDescription());
            return dto;
        }).collect(Collectors.toList());
        model.addAttribute("dbDtoList", dbDtoList);

        // 数据库访问类型编码
        String[] dbAccessTypeCode = DatabaseMeta.dbAccessTypeCode;
        // 数据库访问类型编码说明
        String[] dbAccessTypeDesc = DatabaseMeta.dbAccessTypeDesc;
        List<EnumInfoDTO> accessList = new ArrayList<>();
        for (int i = 0; i < dbAccessTypeCode.length; i++) {
            EnumInfoDTO dto = new EnumInfoDTO();
            dto.setCode(dbAccessTypeCode[i]);
            dto.setValue(dbAccessTypeDesc[i]);
            accessList.add(dto);
        }
        model.addAttribute("accessList", accessList);
        return "jobRepository/jobRepository.index";
    }
}
